package com.sky.utils;

import com.alibaba.fastjson.JSONObject;
import com.sky.exception.PositionException;
import com.sky.properties.PositioningProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.Map;

@Component
public class PositionUtil {

    private static final String USER_STORE_URL="https://api.map.baidu.com/geocoding/v3/?";
    private static final String DISTANCE_URL="https://api.map.baidu.com/direction/v2/driving";


    @Autowired
    private PositioningProperties positioningProperties;


    public void positing(String userAddress){

        //分别获取店铺和用户的坐标

        //店铺坐标
        Map params = new LinkedHashMap<String, String>();
        params.put("address",positioningProperties.getAddress());
        params.put("ak",positioningProperties.getAk());
        params.put("output", "json");

        String response = HttpClientUtil.doGet(USER_STORE_URL, params);
        //解析字符串
        JSONObject jsonObject = JSONObject.parseObject(response);
        //获取返回参数结果
        JSONObject result = jsonObject.getJSONObject("result");
        //从结果中获取地址信息对象
        JSONObject location = result.getJSONObject("location");
        //从地址信息中获取经纬度
        Float lng = location.getFloat("lng");//经度
        Float lat = location.getFloat("lat");//纬度



        //用户坐标
        Map params2 = new LinkedHashMap<String, String>();
        params2.put("address",userAddress);
        params2.put("ak",positioningProperties.getAk());
        params2.put("output", "json");

        String userResponse = HttpClientUtil.doGet(USER_STORE_URL, params2);
        //解析字符串
        JSONObject userJsonObject = JSONObject.parseObject(userResponse);
        //获取返回参数结果
        JSONObject userResult = userJsonObject.getJSONObject("result");
        //从结果中获取地址信息对象
        JSONObject userLocation = userResult.getJSONObject("location");
        //从地址信息中获取经纬度
        Float userLng = userLocation.getFloat("lng");//经度
        Float userLat = userLocation.getFloat("lat");//纬度

        //拼接使用纬度、经度的顺序拼接
        String storeCoordinate = lat+","+lng;
        String userCoordinate = userLat+","+userLng;

        //通过两个经纬度计算距离
        Integer distance = calculateDistance(storeCoordinate, userCoordinate);

        if(distance>positioningProperties.getDistance()){
            throw new PositionException("超过限定范围，无法配送");
        }

    }

    private Integer calculateDistance(String storeCoordinate, String userCoordinate) {

        //构建路径参数
        Map params = new LinkedHashMap<String, String>();
        params.put("origin",storeCoordinate);
        params.put("destination",userCoordinate);
        params.put("output", "json");
        params.put("ak",positioningProperties.getAk());

        //发送请求
        String response = HttpClientUtil.doGet(DISTANCE_URL, params);
        //解析流程response=>result=>routes=>distance
        //结果中包含多个不同出行方式的距离，这里选择常规路线
        JSONObject jsonObject = JSONObject.parseObject(response);

        switch (jsonObject.getInteger("status")){
            case 1: throw new PositionException("服务器歇菜了，无法定位");
            case 2: throw new PositionException("地址无效");
            case 7: throw new PositionException("服务器异常");
        }


        JSONObject result = jsonObject.getJSONObject("result");

        if(result.isEmpty()){
           throw new PositionException("服务器异常");
        }
        JSONObject routes = result.getJSONArray("routes").getJSONObject(0);
        return routes.getInteger("distance");
    }
}
